package com.yxhl.zoume.common.ui.activity.base;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.info.ToolbarType;
import com.yxhl.zoume.core.main.info.HeaderOperationType;
import com.yxhl.zoume.utils.DensityUtils;

import butterknife.BindView;
import butterknife.OnClick;

public abstract class BaseActivityWithToolBarForCar extends BaseActivity {


    @BindView(R.id.appbar_include_car_root)
    AppBarLayout mAppBarLayout;

    @BindView(R.id.toolbar_include_header)
    Toolbar mToolbar;

    /**
     * toolbar返回键
     */
    @BindView(R.id.iv_header_left)
    ImageView mHeaderLeft;
    /**
     * toolbar中间
     */
    @BindView(R.id.rg_include_header_container)
    RadioGroup mHeaderContainerRg;
    @BindView(R.id.rb_include_header_single_way)
    RadioButton mHeaderSingleWayRb;
    @BindView(R.id.rb_include_header_rent_hourly)
    RadioButton mHeaderRentHourlyRb;

    @BindView(R.id.tv_header_title)
    TextView mHeaderTitleTv;

    /**
     * toolbar右侧文本
     */
    @BindView(R.id.tv_header_right)
    TextView mHeaderRightTv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initializeToolBar();
    }

    private void initializeToolBar() {
        //Android 4.4以上状态栏透明(在代码里直接声明透明状态栏更有效)
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);

            mToolbar.setMinimumHeight(DensityUtils.dip2px(this, 73));
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) mToolbar.getLayoutParams();
            marginLayoutParams.setMargins(0, DensityUtils.dip2px(this, 25), 0, 0);
        }

        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);

        mHeaderContainerRg.setOnCheckedChangeListener(new ToolBarOnCheckedChangeListener());
        mToolbar.setNavigationOnClickListener(new onToolbarNavigationClickListener());
    }

    public class onToolbarNavigationClickListener implements View.OnClickListener {

        @Override
        public void onClick(final View v) {
            if (v != null) {
                Animation animation = AnimationUtils.loadAnimation(BaseActivityWithToolBarForCar.this, R.anim.alpha_exit);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        onHeadBackClick(v);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                v.startAnimation(animation);
            }
        }
    }
    /**
     * Toolbar返回图标的点击响应
     */
    protected void onHeadBackClick(View view) {
        finish();
    }
    /**
     * 设置Toolbar导航图标(返回图标)
     *
     * @param drawable
     */
    public void setLeftIcon(Drawable drawable) {
        mHeaderLeft.setImageDrawable(drawable);
    }

    /**
     * 设置Toolbar导航图标(返回图标)
     *
     * @param resId
     */
    public void setLeftIcon(@DrawableRes int resId) {
        mHeaderLeft.setImageResource(resId);
    }

    /**
     * 隐藏Toolbar导航图标(返回图标)
     */
    public void hideLeftIcon() {
        mHeaderLeft.setImageDrawable(null);
    }

    public void setHeaderRightTag(HeaderOperationType operationType) {
        if (mHeaderRightTv != null) {
            mHeaderRightTv.setTag(operationType);
        }
    }

    /**
     * 设置toolbar右侧文本
     *
     * @param operation
     */
    public void setRightOperation(String operation) {
        mHeaderRightTv.setText(operation);
    }


    /**
     * 设置Toolbar的左侧图标,居中标题内容,右侧文本内容
     *
     * @param resId
     * @param headTitle
     * @param operate
     */
    public void setToolbar(@DrawableRes int resId, String headTitle, String operate) {
        if (resId != 0) {
            mHeaderLeft.setImageResource(resId);
        }
        if (headTitle != null) {
            mHeaderTitleTv.setText(headTitle);
        }
        if (operate != null) {
            mHeaderRightTv.setText(operate);
        }
    }

    /**
     * 设置Toolbar的左侧图标,居中标题内容,右侧文本内容
     *
     * @param resId
     * @param headTitle
     * @param operate
     */
    public void setToolbar(@DrawableRes int resId, @StringRes int headTitle, @StringRes int operate) {
        if (resId != 0) {
            mHeaderLeft.setImageResource(resId);
        }
        if (headTitle != 0) {
            mHeaderTitleTv.setText(headTitle);
        }
        if (operate != 0) {
            mHeaderRightTv.setText(operate);
        }
    }

    /**
     * 设置Toolbar的左侧图标,居中标题内容,右侧文本内容
     *
     * @param resId
     * @param headTitle
     * @param operate
     */
    public void setToolbar(@DrawableRes int resId, @StringRes int headTitle, String operate) {
        if (resId != 0) {
            mHeaderLeft.setImageResource(resId);
        }
        if (headTitle != 0) {
            mHeaderTitleTv.setText(headTitle);
        }
        if (operate != null) {
            mHeaderRightTv.setText(operate);
        }
    }

    /**
     * 设置Toolbar的左侧图标,居中标题内容,右侧图标
     *
     * @param resId
     * @param headTitle
     * @param rightResId
     */
    public void setToolbar(@DrawableRes int resId, String headTitle, @DrawableRes int rightResId) {
        if (resId != 0) {
            mHeaderLeft.setImageResource(resId);
        }
        if (headTitle != null) {
            mHeaderTitleTv.setText(headTitle);
        }

        mHeaderRightTv.setText("");
        mHeaderRightTv.setBackgroundResource(rightResId);
    }

    /**
     * 设置Toolbar的左侧图标,居中标题内容,右侧文本内容
     *
     * @param drawable
     * @param headTitle
     * @param operate
     */
    public void setToolbar(Drawable drawable, String headTitle, String operate) {

        mHeaderLeft.setImageDrawable(drawable);

        if (headTitle != null) {
            mHeaderTitleTv.setText(headTitle);
        }
        if (operate != null) {
            mHeaderRightTv.setText(operate);
        }
    }

    public void setToolbarTitle(@StringRes int title) {
        if (title != 0) {
            mHeaderTitleTv.setText(title);
        }
    }

    public void setToolbarTitle(String title) {
        if (title != null) {
            mHeaderTitleTv.setText(title);
        }
    }

    public void setToolbarOperation(@StringRes int operation) {
        if (operation != 0) {
            mHeaderRightTv.setText(operation);
        }
    }

    public void setToolbarOperation(String operation) {
        if (operation != null) {
            mHeaderRightTv.setText(operation);
        }
    }

    public void setToolbar(@StringRes int title, @StringRes int operation) {
        setToolbar(0, title, operation);
    }

    public void setToolbar(String title, String operation) {
        setToolbar(0, title, operation);
    }

    public void setToolbar(@StringRes int title, String operation) {
        setToolbar(0, title, operation);
    }

    /**
     * 改变Toolbar样式
     *
     * @param type
     */
    public void changeToolbarInto(ToolbarType type) {
        if (type != null) {
            switch (type) {
                case DEFAULT:
                    mHeaderContainerRg.setVisibility(View.GONE);
                    mHeaderTitleTv.setVisibility(View.VISIBLE);
                    break;
                case SINGLE_WAY:
                    mHeaderContainerRg.setVisibility(View.VISIBLE);
                    mHeaderTitleTv.setVisibility(View.GONE);
                    mHeaderSingleWayRb.setChecked(true);
                    break;
                case RENT_HOURLY:
                    mHeaderContainerRg.setVisibility(View.VISIBLE);
                    mHeaderTitleTv.setVisibility(View.GONE);
                    mHeaderRentHourlyRb.setChecked(true);
                    break;
            }
        }
    }


    /**
     * 左侧点击监听
     *
     * @param v
     */
    @OnClick(R.id.iv_header_left)
    protected void onHeaderLeft(final View v) {
        if (v != null) {
            Animation animation = AnimationUtils.loadAnimation(BaseActivityWithToolBarForCar.this, R.anim.alpha_exit);
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    onHeaderLeftClick(v);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
            v.startAnimation(animation);
        }
    }

    /**
     * 右侧点击监听
     *
     * @param v
     */
    @OnClick(R.id.tv_header_right)
    protected void onHeaderRight(View v) {
        if (v != null) {
            onHeaderRightClick(v);
        }
    }

    /**
     * Toolbar返回图标的点击响应
     */
    protected abstract void onHeaderLeftClick(View view);

    /**
     * Toolbar右侧文本的点击响应
     */
    protected abstract void onHeaderRightClick(View view);

    /**
     * toolbar中央check切换响应
     *
     * @param group
     * @param checkedId
     */
    protected abstract void onHeaderCheckedChange(RadioGroup group, int checkedId);

    class ToolBarOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            onHeaderCheckedChange(group, checkedId);
        }
    }


}
